package com.hellpro.lolbox.moment.model;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hellpro.lolbox.moment.bean.MomentContentBean;
import com.hellpro.lolbox.utils.OkHttpClientUtils;

import java.io.IOException;
import java.lang.reflect.Type;


/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/22
 * 备注:
 */

public class LoadContentInfo {


    private String mUrl;
    private Handler mHandler;
    private Context mContext;
    private  int flag;

    public LoadContentInfo(String url ,Handler handler,int flag) {
        this.mUrl = url;
        this.mHandler = handler;
//        this.mContext = context;
        this.flag = flag;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/19
     * 备注:分类导航下载并解析数据
     */

    public void startLoadHotInfo() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    String json = OkHttpClientUtils.getStringFromURL(mContext, mUrl, null);
                    //使用gson进行数据解析
                    Gson gson = new Gson();
                    Type type = TypeToken.get(MomentContentBean.class).getType();
                    MomentContentBean bean = gson.fromJson(json, type);
//                    EventBus.getDefault().post(bean);
                    Message msg = Message.obtain();
                    msg.what = 5;
                    msg.obj = bean;
                    msg.arg1 = flag;
                    mHandler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
